import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 徐海涛
 * Date: 2022-02-22
 * Time: 13:33
 */
public class TestDemo {
    public static void main(String[] args) {
        int[] array = new int[1_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(1000);
        }
        Map<Integer,Integer> ret = func1(array);
        System.out.println(ret);
    }

    //给定1w个数据，统计每个数据出现的次数
    public static Map<Integer,Integer> func1 (int[] array) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int x :array) {
            if(map.get(x) == null) {
                map.put(x,1);
            }else {
                int val = map.get(x);
                map.put(x,val+1);
            }
        }
        return map;
    }

    //将10w个数据去重
    public static Set<Integer> func2(int[] array) {
        HashSet<Integer> set = new HashSet<>();
        for (int x : array) {
            set.add(x);
        }
        return set;
    }

    //从10w个数据中，找出第一个重复的元素
    public static int func3(int[] array) {
        HashSet<Integer> set = new HashSet<>();
        for (int x : array) {
            if(set.contains(x)) {
                return x;
            }else {
                set.add(x);
            }
        }
        return -1;
    }

    //只出现一次的数字
    public int singleNumber1(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int x : nums) {
            if(set.contains(x)) {
                set.remove(x);
            }else {
                set.add(x);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;
    }




}
